This week's focus was on programming a microcontroller. The individual assignment was to read a microcontroller datasheet and program a board to do something, using at least two different development workflows. The group assignment was to compare the performance and development workflows for different microcontroller architectures.
Our full group documentation, where we compare different microcontrollers and their development environments, can be found on our group assignment page.
For our group assignment, we compared two main programming approaches: MicroPython and the Arduino (C/C++) framework.
In short, we found a clear trade-off: MicroPython is superior for rapid testing and real-time data, while Arduino is the better choice when final performance and speed are the priority.
For my individual assignment, I will be programming the XIAO ESP32-C3 board, which features the ESP32-C3 microcontroller from Espressif. I will first simulate the task on Wokwi to see how it works, and then I will implement it on the actual hardware using two different workflows: the Arduino IDE and "bare-metal" C using Visual studio code.
The first step was to understand the board and its microcontroller. The XIAO ESP32-C3 uses the ESP32-C3FN4 chip from Espressif. This is a 32-bit RISC-V single-core processor, which is a significant step up from the 8-bit AVR chips.
| Feature | Specification |
|---|---|
| CPU | RISC-V 32-bit single-core |
| Max Frequency | 160 MHz |
| On-Chip Memory | 400 KB SRAM + 4 MB Flash |
| Wireless | 2.4GHz Wi-Fi (802.11 b/g/n) and Bluetooth 5 (LE) |
| GPIOs | Up to 22 (11 available on the XIAO) |
| Interfaces | I2C, I2S, SPI, UART, ADC |
| Operating Voltage | 3.3V |
The most critical information for programming is the pinout. I needed to map the physical pins on the tiny XIAO board to their GPIO (General Purpose Input/Output) numbers and their names in the Arduino IDE.
| XIAO Pin | Arduino Name | GPIO Number | Functions |
|---|---|---|---|
| D0 | `D0` / `A0` | GPIO2 | ADC1_CH2, UART1_TXD |
| D1 | `D1` / `A1` | GPIO3 | ADC1_CH3 |
| D2 | `D2` / `A2` | GPIO4 | ADC1_CH4, I2C SDA |
| D3 | `D3` / `A3` | GPIO5 | ADC1_CH5, I2C SCL |
| D4 | `D4` | GPIO6 | SPI_CLK |
| D5 | `D5` | GPIO7 | SPI_MISO |
| D6 | `D6` | GPIO21 | UART0_TX (Serial) |
| D7 | `D7` | GPIO20 | UART0_RX (Serial) |
| D8 | `D8` | GPIO8 | SPI_MOSI, BOOT Button |
| D9 | `D9` | GPIO9 | SPI_CS |
| D10 | `D10` | GPIO10 | SPI_HD |
| LED | (No Arduino Name) | GPIO8 | Onboard User LED (Blue) |
Before working on the actual hardware, I simulated my program on Wokwi, an online simulator for microcontrollers.
I started off simple by first just connecting a push button with an Led and trying to simulate that. I connected the led cathode to ground and the anode to a resistor and pin 9. The button to ground and pin 8. Here I encountered an error because in the code I had actually used pin 9 for the button and pin 8 for the led. However, there were a few errors first this was not the xiao c3 and moreover the pin 8 was occupied by the board for on board LED which caused nothing to happen once the button was pressed.
First I changed the board to the xiao ESP32-C3 then wrote the code again and did the wiring. However, I didn't assign D to the pin numbers becasue of which the code didn't work and I spent quite a lot of time changing the circuit and then finally when i added the D to make it D2 and D3 it finally worked. After fixing the pin numbers I was able to get the led to turn off when the button was pressed. Here is the code I used for the simulation:
const int buttonPin = D2; // pushbutton pin
const int ledPin = D3; // LED pin
int buttonState = 0;
void setup() {
Serial.begin(115200);
// enable internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// LED output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton (LOW = pressed)
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// button pressed?
if (buttonState == HIGH) { // active LOW
digitalWrite(ledPin, HIGH); // turn LED on
} else {
digitalWrite(ledPin, LOW); // turn LED off
}
delay(1000);
}
Here is what diagram looked like:
After successfully simulating the program on Wokwi, I moved on to implementing it on the actual XIAO ESP32-C3 hardware on breadboard using the Arduino IDE.I first downloaded the Arduino IDE from the offical website and then downloaded the esp32 board support package from the board manager which took some time to intall. The wiring was the same as in the simulation: the button connected to pin D2 and ground, and the LED connected to pin D3 with a current-limiting resistor to ground. I used male to male jumped cables to connect everything with the GND to the breadboard ground rails and the rest as before.
This is what the circuit looked like:
← Back to Main Page